package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Elastic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import flashx.textLayout.accessibility.TextAccImpl;

	public class Hud
	{
		
		private var updatePlayerCash:Boolean = false;		
		public var doneButton:Sprite;
		private var bottomBar:Sprite;
		private var shopBar:ShopBar;
		private var coinTf:TextField;
		
		private var bottomBars:Vector.<Sprite> = new Vector.<Sprite>;
		
		public static const PADDING:int = 10;
		
		private var m_player:Player;
		public var display:Sprite = new Sprite();
		
		private var updateTimer:Timer = new Timer(1000,0);
		private var tickTimer:Timer = new Timer(1000/60,0);
		
		public var m_lastTotalCash:int;
		public var m_lastPlayerCash:Number;
		public var dirty:Boolean = false;
		
		public var addStoreButton:Sprite;
		public var addShopperButton:Sprite;
		public var expandButton:Sprite
		
		public var totalDeltaCash:int;
		public var m_lastUpdateTick:int;
		public var cashPerSecond:Number;
		public var m_startTime:int;
		
		private var buttonsArea:Sprite;
		private var topRightAnchor:Sprite;
		
		private var maintenance:Sprite = new Sprite();
		private var maintenance_TF:TextField = new TextField();
		
		private var m_restockTF:TextField = new TextField();
		private var m_totalShoppers:Sprite = new Sprite();
		private var m_totalShopperAmount:TextField = new TextField();
		
		private var m_tooltip:Sprite = new Sprite();
		private var m_cancelButton:Sprite = new Sprite();
		
		private var m_game:Game;
		public function Hud(game:Game)
		{
			m_game = game;
			initPrimaryDisplayEventListeners();
			m_lastPlayerCash = game.player.cash;
			m_startTime = getTimer();
			m_player = game.player;
			

			
			updateTimer.addEventListener(TimerEvent.TIMER, onUpdate);
			display.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			updateTimer.start();
			
			topRightAnchor = new Sprite();
			addStoreButton = Util.getButton('Buy Shops',0,.7);
			addShopperButton = Util.getButton('Get Shoppers',0,.7);
			expandButton = Util.getButton('Expand Mall',0,.7);
			
			
			addShopperButton.y = addStoreButton.height + Game.PADDING*2;
			addStoreButton.y = Game.PADDING;
			expandButton.y = addShopperButton.y + addShopperButton.height + PADDING;
						
			Util.buttonize(addStoreButton);
			topRightAnchor.addChild(addStoreButton);
			topRightAnchor.addChild(addShopperButton);
			topRightAnchor.addChild(expandButton);
			
			display.addChild(topRightAnchor);
//			
//			
//			var button:Sprite;
//			
//			button = Util.makeButton('assets/buttons/shopping_cart.png');
//			button.addEventListener(MouseEvent.CLICK, function():void{
//				Game.instance.command("place_scaffold");
//			});
//			buttonsArea.addChild(button);
//			
//			button = Util.makeButton('assets/buttons/shopping_cart.png');
//			button.x = button.width+2;
//			buttonsArea.addChild(button);
//
//			display.addChild(buttonsArea);
			
			
			
			var lookup:Dictionary = new Dictionary(true);
			var friendBar:Sprite = new Sprite();
			// Friends pics to botom bar
			var snUsers:Vector.<SNUser> = getSNUsers();
			var i:int, length:int;
			for(i = 0, length = snUsers.length; i < length; i++){
				var pic:DisplayObject = ImageManager.getImage(snUsers[i].img);
				var snUserSlot:Sprite = Util.getRoundRectSprite(pic.width + PADDING, pic.height + PADDING, Math.random()*0x777777 + 0x777777, .8, 10);
				Util.buttonize(snUserSlot);
				lookup[snUserSlot] = snUsers[i];
				snUserSlot.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{
					Game.instance.buyPlayer(lookup[me.currentTarget]);
				});
				snUserSlot.addChild(pic);
				pic.x = snUserSlot.width/2 - pic.width/2;
				pic.y = PADDING/2;
				friendBar.addChild(snUserSlot);
				snUserSlot.x = PADDING*i + i*snUserSlot.width;
			}
			
			bottomBar = Util.getRoundRectSprite(friendBar.width + PADDING, friendBar.height + PADDING, 0, .7, 10);
			bottomBar.addChild(friendBar);
			display.addChild(bottomBar);
			
			
			shopBar = new ShopBar(friendBar.width*1.5, friendBar.height*1.5);
			//var shopBarBG:Sprite = Util.getRoundRectSprite(friendBar.width*1.5, friendBar.height*1.5, 0, .7, 10);
			
			//shopBar.addCHild
			shopBar.display.visible = false;
			display.addChild(shopBar.display);			
			
			bottomBars.push(bottomBar);
			bottomBars.push(shopBar.display);			
			
			friendBar.x = bottomBar.width/2 - friendBar.width/2;
			friendBar.y = PADDING/2;
			
			buttonsArea = new Sprite();
			var coinBar:Sprite = Util.getRoundRectSprite(150,40,0,0);
			var coinIcon:DisplayObject = ImageManager.getImage('assets/coins.png');
			var pad:int = (coinBar.height - coinIcon.height)/2;
			coinIcon.y = pad;
			coinIcon.x = pad;
			coinIcon.filters = [new GlowFilter(0,1,5,5,5)];
			coinBar.addChild(coinIcon);
			
			Util.applyStyleH1(m_restockTF);
			display.addChild(m_restockTF);
			m_restockTF.y = coinBar.height + Game.PADDING;			
			m_restockTF.x = Game.PADDING;
			
			coinTf = new TextField();
			Util.applyStyleH1(coinTf);
			coinTf.text = '$'+m_player.cash;
			coinTf.autoSize = TextFieldAutoSize.LEFT;
			
			coinBar.addChild(coinTf);
			coinTf.x = coinIcon.x + coinIcon.width + pad;
			coinTf.y = coinBar.height/2 - coinTf.height/2;
			
			coinBar.x = PADDING;
			coinBar.y = PADDING;
			display.addChild(coinBar);
			
			display.addChild(m_totalShoppers);
			var totalShoppersIcon:DisplayObject = ImageManager.getImage('assets/shopper1_sheet.png');
			m_totalShoppers.addChild(totalShoppersIcon);
			totalShoppersIcon.scrollRect = new Rectangle(0,0,32,32);
			totalShoppersIcon.scaleX = 1.25;
			totalShoppersIcon.scaleY = 1.25;			
			totalShoppersIcon.filters = [new GlowFilter(0,1,3,3,5)];
			
			
			Util.applyStyleH1(m_totalShopperAmount);
			m_totalShopperAmount.text = String(m_game.getNumberShoppers());
			m_totalShopperAmount.autoSize = TextFieldAutoSize.LEFT;
			m_totalShopperAmount.y = totalShoppersIcon.scrollRect.height/2 - m_totalShopperAmount.height/2;
			m_totalShoppers.addChild(m_totalShopperAmount);
			m_totalShopperAmount.x = 32 + Game.PADDING;
			m_totalShoppers.x = coinBar.width + Game.PADDING*2;
			m_totalShoppers.y = Game.PADDING;
			
			var maintenance_icon:DisplayObject = ImageManager.getImage('assets/wrench.png');
			maintenance_icon.filters = [new GlowFilter(0,1,4,4,5)];
			maintenance.addChild(maintenance_icon);
			Util.applyStyleH1(maintenance_TF);
			maintenance_TF.text = game.mall.maintenance + '%';
			maintenance_TF.x = maintenance_icon.width + Game.PADDING;
			maintenance.addChild(maintenance_TF);
			maintenance.x = 300;
			maintenance.y = Game.PADDING;
			display.addChild(maintenance);
			
			doneButton = Util.getButton('Done!',0xff0000,.8);
			doneButton.scaleX = 0;
			doneButton.scaleY = 0;
			doneButton.visible = false;
			display.addChild(doneButton);
			
			initTooltip();
			
			var cbutton:DisplayObject = ImageManager.getImage('assets/buttons/cancel96.png');
			m_cancelButton.addChild(cbutton);
			m_cancelButton.addEventListener(MouseEvent.CLICK, onCancelClick);
			Util.buttonize(m_cancelButton);
			display.addChild(m_cancelButton);
			
			display.addChild(m_mainDialog);
			
			onResize(new Event(''));
			Game.stage.addEventListener(Event.RESIZE, onResize);
			
		}
		
		private var m_tooltipBG:Sprite = new Sprite();
		private var m_tooltipTF:TextField = new TextField();
		
		private var m_mainDialog:Sprite = new Sprite();
		private var m_mainDialogIsOpen:Boolean = false;
		
		public function doTutorialStep(step:int):void{
			switch(step){
				case 0:
					addStoreButton.visible = false;
					addShopperButton.visible = false;
					m_restockTF.visible = false;
					m_totalShoppers.visible = false;
					expandButton.visible = false;
					maintenance.visible = false;
					addStoreButton.filters = [new GlowFilter(0xffffff)];
					break;
				case 1:
					if(addShopperButton.visible == false){
						addStoreButton.filters = [];
						addShopperButton.alpha = 0;
						addShopperButton.visible = true;
						addShopperButton.filters = [new GlowFilter(0xffffff)];
						addShopperButton.x -= 200;
						TweenLite.to(addShopperButton, 1.5, {alpha: 1, x: addShopperButton.x + 200, ease: Bounce.easeOut});
						
						m_totalShoppers.alpha = 0;
						m_totalShoppers.visible = true;
						TweenLite.to(m_totalShoppers, 2, {alpha: 1});						
					}
					break;
				case 2:
					if(m_restockTF.visible == false){
						addShopperButton.filters = [];
						m_restockTF.alpha = 0;
						m_restockTF.visible = true;
						m_restockTF.y += 200;
						TweenLite.to(m_restockTF, 1.5, {alpha: 1, y: m_restockTF.y - 200, ease: Bounce.easeOut});					
					}
					break;
				case 4:
					if(expandButton.visible == false){
						addShopperButton.filters = [];
						expandButton.alpha = 0;
						expandButton.visible = true;
						TweenLite.to(expandButton, 1.5, {alpha: 1});
					}
					break;				
			}
		}
		
		public function showAsMainDialog(dispObj:DisplayObject):void{
			if(!m_mainDialogIsOpen){
				_showAsMainDialog(dispObj);
			} else {
				TweenLite.to(m_mainDialog, .5, {scaleX: 0, scaleY: 0,
					onComplete: function():void{
						_showAsMainDialog(dispObj);
					}
				});
			}
		}
		
		private function _showAsMainDialog(dispObj:DisplayObject):void{
			m_mainDialogIsOpen = true;
			Util.removeChildren(m_mainDialog);
			m_mainDialog.addChild(dispObj);
			dispObj.x = -dispObj.width/2;
			dispObj.y = -dispObj.height/2;
			//onResize(new Event(''));
			m_mainDialog.scaleX = 0;
			m_mainDialog.scaleY = 0;
			TweenLite.to(m_mainDialog, .5, {scaleX: 1, scaleY: 1});			
		}
		
		
		public function hideMainDialog():void{
			m_mainDialogIsOpen = false;
			TweenLite.to(m_mainDialog, .5, {scaleX: 0, scaleY: 0});
		}
		
		private function onCancelClick(me:MouseEvent):void{
			Game.instance.cancelClick();
		}
		
		private function initPrimaryDisplayEventListeners():void{
			display.addEventListener(MouseEvent.MOUSE_MOVE, onHudMouseMove);			
		}
		
		private function onHudMouseMove(me:MouseEvent):void{
			Game.instance.inputManager.onMouseMove(me);
		}
		
		private function initTooltip():void{
			m_tooltip.addChild(m_tooltipBG);
			Util.applyStyleH3(m_tooltipTF);
			m_tooltip.addChild(m_tooltipTF);
			m_tooltipTF.x = Game.PADDING/2;
			m_tooltipTF.y = Game.PADDING/2;
			display.addChild(m_tooltip);
		}
		
		public function showTooltip(string:String, location:Point):void{
			m_tooltip.visible = true;
			m_tooltipTF.text = string;
			m_tooltipTF.autoSize = TextFieldAutoSize.LEFT;
			Util.getRoundRectSprite(m_tooltipTF.width + Game.PADDING, m_tooltipTF.height + Game.PADDING, 0, .5, 10, 0, 0, m_tooltipBG);
			m_tooltip.x = location.x;
			m_tooltip.y = location.y;
		}
		
		public function hideTooltip():void{
			m_tooltip.visible = false;
		}
		
		private var m_confirmDialog:Sprite;
		
		public function updateNumberShoppers():void{
			m_totalShopperAmount.text = String(Game.instance.getNumberShoppers());
			m_totalShopperAmount.autoSize = TextFieldAutoSize.LEFT;
		}
		
		public function confirmationDialog(str:String, callback:Function):void{
			if(m_confirmDialog != null){
				m_confirmDialog.parent.removeChild(m_confirmDialog);
				m_confirmDialog = null;
			}
			
			var confirm:Boolean = false;
			//m_confirmationCallback = callback;
			var buttons:Sprite = new Sprite();
			var yesButton:Sprite = Util.getButton('Yes', 0x00ff00, 1);
			yesButton.addEventListener(MouseEvent.CLICK, 
				function(me:MouseEvent):void{
					callback(true);
					Util.scale(m_confirmDialog, 1, 0, .5);
				},
				false,
				0,
				true
			);			
			var noButton:Sprite = Util.getButton('No', 0xff0000, 1);
			noButton.addEventListener(MouseEvent.CLICK, 
				function(me:MouseEvent):void{
					callback(false);
					Util.scale(m_confirmDialog, 1, 0, .5);
				},
				false,
				0,
				true
			);
			m_confirmDialog = new Sprite();
			var confirmDialogBG:Sprite = Util.getRoundRectSprite(300,100, 0x000077, .8, 20);
			m_confirmDialog.addChild(confirmDialogBG);
			
			var txt:TextField = new TextField();
			Util.applyStyleH2(txt);
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.width = m_confirmDialog.width;
			txt.htmlText = '<p align="center">' + str + '</p>';
			txt.wordWrap = true;
			txt.y = Game.PADDING;
			txt.alpha = 0;
			buttons.addChild(yesButton);
			buttons.addChild(noButton);
			noButton.x = yesButton.width + Game.PADDING;
			buttons.y = confirmDialogBG.height - buttons.height - Game.PADDING;
			buttons.x = confirmDialogBG.width/2 - buttons.width/2;
			m_confirmDialog.addChild(buttons);
			m_confirmDialog.addChild(txt);
			
			display.addChild(m_confirmDialog);
			m_confirmDialog.x = display.width/2 - m_confirmDialog.width/2;
			m_confirmDialog.y = display.height/2 - m_confirmDialog.height/2;
			
			Util.scale(m_confirmDialog, 0, 1, .25,1,1,null,
				function():void{
					TweenLite.to(txt, .25, {alpha: 1});
				}
			);
		}
		
		public function showBuyShopsBar():void{
			showBottomSprite(shopBar.display);
		}
		
		public function showBottomSprite(sprToShow:Sprite):void{
			var i:int, l:int;
			for(i = 0, l = bottomBars.length; i < l; i++){
				var spr:Sprite = bottomBars[i];
				if(spr != sprToShow){
					TweenLite.to(spr, .5, {alpha: 0});
				} else {
					sprToShow.alpha = 0;
					sprToShow.visible = true;
					TweenLite.to(sprToShow, 1, {alpha: 1});
				}
			}
		}
		
		private function getSNUsers():Vector.<SNUser>{
			var snUsers:Vector.<SNUser> = new Vector.<SNUser>
			snUsers.push(new SNUser('Vinh', 'assets/profile_pics/vinh_t.jpg'));
			snUsers.push(new SNUser('Brian', 'assets/profile_pics/brian_r.jpg'));
			snUsers.push(new SNUser('Helen', 'assets/profile_pics/helen_z.jpg'));
			snUsers.push(new SNUser('Jesse', 'assets/profile_pics/jesse_smith.jpg'));
			snUsers.push(new SNUser('Mark', 'assets/profile_pics/mark_n.jpg'));
			snUsers.push(new SNUser('Paul', 'assets/profile_pics/paul_s.jpg'));
			
			return snUsers;
		}

		private function updateBottomBar():void{
			bottomBar.y = Game.stage.stageHeight - bottomBar.height - PADDING;
			bottomBar.x = Game.stage.stageWidth/2 - bottomBar.width/2;
		}
		
		private function updateShopBar():void{
			shopBar.display.y = Game.stage.stageHeight - shopBar.display.height - PADDING;
			shopBar.display.x = Game.stage.stageWidth/2 - shopBar.display.width/2;			
		}
		
		public function hideDoneButton():void{
			var finalPoint:Point = new Point(doneButton.x + doneButton.width/2, doneButton.y + doneButton.height/2);
			TweenLite.to(doneButton, .25, {scaleX: 0, scaleY: 0, x: finalPoint.x, y: finalPoint.y,
				onComplete: function():void{
					doneButton.visible = false;
				}
			});
		}
		
		public function showDoneButton():void{
			doneButton.visible = true;
			doneButton.scaleX = 1;
			doneButton.scaleY = 1;
			var finalPoint:Point = new Point(doneButton.x - doneButton.width/2, doneButton.y - doneButton.height/2);
			doneButton.scaleX = 0;
			doneButton.scaleY = 0;
			TweenLite.to(doneButton, .25, {scaleX: 1, scaleY: 1, x: finalPoint.x, y: finalPoint.y});
		}
		
		private function onResize(e:Event):void{
			buttonsArea.x = -buttonsArea.width + Game.stage.stageWidth - PADDING;
			buttonsArea.y = -buttonsArea.height + Game.stage.stageHeight - PADDING;
			
			doneButton.x = Game.stage.stageWidth/2 - doneButton.width/2;
			doneButton.y = 100;
			
			topRightAnchor.x = Game.stage.stageWidth - topRightAnchor.width;
			
			m_cancelButton.y = Game.stage.stageHeight - m_cancelButton.height - Game.PADDING;;
			m_cancelButton.x = Game.PADDING;
			
			m_mainDialog.x = Game.stage.stageWidth/2;
			m_mainDialog.y = Game.stage.stageHeight/2;
			
			updateBottomBar();
			updateShopBar();
		}

		private function onEnterFrame(e:Event):void{
			if(updatePlayerCash){
				m_lastPlayerCash +=  .2*(m_player.cash - m_lastPlayerCash);
				
				coinTf.text = '$' + Math.round(m_lastPlayerCash);
			}
			
			var timeTillRestock:int = Game.instance.mall.getTimeUntilRestock();
			m_restockTF.text = "Restock in: " + Math.round(timeTillRestock/1000) + "s";
			m_restockTF.autoSize = TextFieldAutoSize.LEFT;	
			
			maintenance_TF.text =  printf("%.2f %", Game.instance.mall.maintenance);
			maintenance_TF.autoSize = TextFieldAutoSize.LEFT;
		}
		
		private function onUpdate(te:TimerEvent):void{
			if(Math.abs(m_lastPlayerCash - m_player.cash) > 1 && dirty == false){
				updatePlayerCash = true;
			} else {
				updatePlayerCash = false;
			}
		}
	}
}